<template>
  <div>
    <shop_header :obj="details" @changTabName="curNameFn"></shop_header>
    <div class="scores">
      <div class="left">
        <h2>{{ Math.floor(scores.overall_score * 10) / 10 }}</h2>
        <p>综合评价</p>
        <span>{{
          "高于周边商家" +
          Math.round(scores.compare_rating * 100 * 10) / 10 +
          "%"
        }}</span>
      </div>
      <div class="right">
        <div>
          服务态度
          <span class="star"
            ><van-rate
              readonly
              allow-half
              v-model="scores.service_score"
              :size="10"
              color="#ffd21e"
              void-icon="star"
              void-color="#eee"
          /></span>
          <span>{{ Math.round(scores.service_score * 10) / 10 }}</span>
        </div>
        <div>
          服务态度
          <span class="star"
            ><van-rate
              readonly
              allow-half
              v-model="scores.food_score"
              :size="10"
              color="#ffd21e"
              void-icon="star"
              void-color="#eee"
          /></span>
          <span>{{ Math.round(scores.food_score * 10) / 10 }}</span>
        </div>
        <div>
          送达时间 <span>{{ scores.deliver_time }}分钟 </span>
        </div>
      </div>
    </div>
    <div class="list">
      <button
        v-for="(item, index) in list"
        :key="item.id"
        :class="{ unsatisfied: item.unsatisfied, current: index == current }"
        @click="btn(index, item.name)"
      >
        {{ item.name + "(" + item.count + ")" }}
      </button>
    </div>

    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <ul class="remakes-list">
          <li v-for="item in remakesList" :key="item.id">
            <img
              :src="
                item.avatar ? item.avatar : '//elm.cangdu.org/img/default.jpg'
              "
              alt=""
            />
            <div class="user">
              <div class="username">
                <span class="name">{{ item.username }}</span>
                <span class="time">{{ item.rated_at }}</span>
              </div>
              <div class="rating_star">
                <span class="star"
                  ><van-rate
                    readonly
                    allow-half
                    v-model="item.rating_star"
                    :size="5"
                    color="#ffd21e"
                    void-icon="star"
                    void-color="#eee"
                /></span>
                <span style="margin-left: 15px; font-size: 12px; color: #ccc">{{
                  item.time_spent_desc
                }}</span>
              </div>
              <img
                v-show="i.image_hash"
                alt=""
                v-for="(i, index) in item.item_ratings"
                :src="i.image_hash"
                :key="index"
              />
              <div class="food">
                <span v-for="food in item.item_ratings" :key="food.id">{{
                  food.food_name
                }}</span>
              </div>
            </div>
          </li>
        </ul>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import shop_header from "../components/shop-header.vue";
import { shop, scores, classification, remakes } from "../api/shop";
export default {
  components: { shop_header },
  data() {
    return {
      details: {},
      remakesList: [],
      scores: {},
      list: [],
      current: 0,
      num: 0,
      remake: {
        id: this.$route.query.id,
        tag_name: "",
        offset: 0,
        limit: 10,
      },
      isLoading: false,
      loading: false,
      finished: false,
      tabName: 1,
    };
  },
  created() {
    shop(this.remake.id).then((res) => {
      this.details = res.data;
    });
    scores(this.remake.id).then((res) => {
      this.scores = res.data;
    });
    classification(this.remake.id).then((res) => {
      this.list = res.data;
    });
    this.getList();
  },
  methods: {
    curNameFn(val) {
      console.log(val);
      this.tabName = val;
    },
    resetData() {
      this.remakesList = [];
      this.num = 0;
      this.finished = false;
    },
    getList() {
      remakes(this.remake).then((res) => {
        this.isLoading = false;
        this.loading = false;
        // this.finished = true;
        // this.remakesList = res.data;
        res.data.forEach((item) => {
          if (item.avatar) {
            item.avatar = item.avatar.split("");
            item.avatar.splice(3, 0, "/");
            item.avatar.splice(1, 0, "/");
            item.avatar = item.avatar.join("");
            item.avatar =
              "https://fuss10.elemecdn.com/" + item.avatar + ".jpeg";
            // console.log(item.avatar);
          }
          item.item_ratings.forEach((a) => {
            if (a.image_hash) {
              a.image_hash = a.image_hash.split("");
              a.image_hash.splice(3, 0, "/");
              a.image_hash.splice(1, 0, "/");
              a.image_hash = a.image_hash.join("");
              a.image_hash =
                "https://fuss10.elemecdn.com/" + a.image_hash + ".jpeg";
            }
          });
        });
        this.remakesList = [...this.remakesList, ...res.data];
      });
    },

    btn(id, name) {
      this.current = id;
      this.remake.tag_name = name;
      this.getList();
    },
    onLoad() {
      this.num++;
      this.remake.offset = this.num * 10;
      this.getList();
    },
    onRefresh() {
      this.resetData();
      this.getList();
    },
  },
};
</script>

<style lang="less">
* {
  margin: 0;
  padding: 0;
}
.scores {
  display: flex;
  height: 100px;
  border-top: 1px solid #ccc;
  margin-top: 200px;
  .left {
    flex: 1;

    text-align: center;
    h2 {
      margin-top: 15px;
      color: #ff8939;
    }
    p {
      color: #9b9b9b;
    }
    span {
      color: #ccc;
      font-size: 12px;
    }
  }
  .right {
    margin-top: 15px;
    margin-left: 50px;

    flex: 2;

    color: #787878;
    span {
      font-size: 12px;
      color: #666;
    }
    .star {
      margin: 10px 5px;
    }
  }
}
.list {
  border-top: 1px solid #ccc;
  padding-top: 25px;
  button {
    border: none;
    background-color: #ebf5ff;
    padding: 0 5px;
    margin: 10px;
    height: 30px;
    color: #787878;
    border-radius: 3px;
  }

  .unsatisfied {
    color: #aaaaaa;
    background-color: #e5e5e5;
  }
  .current {
    color: #fff;
    background-color: #3190e8;
  }
}
.remakes-list {
  margin-top: 30px;
  li {
    display: flex;
    height: 170px;
    > img {
      margin-left: 15px;
      width: 30px;
      height: 30px;
      border-radius: 50%;
    }
    .user {
      flex: 6;
      .name {
        margin-left: 10px;
      }
      .time {
        float: right;
        margin-right: 10px;
        color: #ccc;
        font-size: 12px;
      }
      img {
        width: 90px;
        height: 90px;
        margin-left: 15px;
      }
      .food {
        margin-left: 15px;
        font-size: 12px;
        color: #999;
        margin-top: 10px;
        span {
          display: inline-block;
          width: 50px;
          height: 20px;
          line-height: 20px;
          font-size: 14px;
          border: 1px solid #ccc;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          margin-right: 15px;
          border-radius: 3px;
        }
      }
    }
    .rating_star {
      margin-left: 10px;
    }
  }
}
/deep/.van-sticky {
  position: fixed;
  top: 105px;
  left: 0;
  width: 100%;
}
</style>